package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.zhiqianbao.R;

/**
 * Created by liang on 2016/1/28.
 */
public class RepayTabHostActivity extends BaseActivity {
    ViewPager pager;
    ViewPagerAdapter adapter;

    Fragment beforeRepayFragment = new BeforeRepayFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_tabhost);
        pager = (ViewPager) this.findViewById(R.id.pager);

        // init view pager
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        Fragment fragment = null;
//        fragment = beforeRepayFragment;
//        fragment =  afterRepayFragment;
    }
    private class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }
        public Fragment getItem(int num) {
            Fragment fragment = null;
            switch (num) {
                case 0 :
                    fragment = beforeRepayFragment;
                    break;
            }
            return fragment;
        }
        @Override
        public int getCount() {
            return 1;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0 :
                    return "应还款";
            }
            return "";
        }
    }
    long mExitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Object mHelperUtils;
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
                mHomeIntent.addCategory(Intent.CATEGORY_HOME);
                mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                startActivity(mHomeIntent);
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            if(requestCode== BeforeRepayFragment.REQUEST_CODE){
                beforeRepayFragment.onActivityResult(requestCode,resultCode,data);
            }
        }
    }
}
